package com.amt.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 江伟
 * @Title: ybapplet
 * @Package com.amt.util
 * @Description: 订单支付工具类
 * @date 2021/2/4 17:20
 * Copyright (c) ©1994-2021 Scjydz.com All Rights Reserved.
 */
@Component
@Slf4j
public class OrderPayUtil {

    public  String message;

    /***
     * @Description;  验证是否在限制时间内下单
     * @param time 限制时间
     * @return true 是  false否
    */
    public boolean validatePayTime(String time) {
        long now = SystemClockUtil.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String sTime = null;
        String eTime = null;
        if(StringUtil.isNotEmpty(time)) {
            String[] times = time.split("@");
            sTime = DateFormatUtils.format(date, "yyyy-MM-dd") + " "+times[1];
            eTime = DateFormatUtils.format(date, "yyyy-MM-dd") + " "+times[0];
            try {
                if (now >= sdf.parse(sTime).getTime() || now <= sdf.parse(eTime).getTime()) {
                    message = "当前时间不能缴费，请在"+times[0]+" - "+times[1]+"内操作";
                    return true;
                }
            } catch (ParseException e1) {
                log.error("创建日期格式化异常{}", e1);
                return true;
            }
        } else {
            sTime = DateFormatUtils.format(new Date(), "yyyy-MM-dd")+" 22:00:00";
            eTime = DateFormatUtils.format(new Date(), "yyyy-MM-dd")+" 07:00:00";
            try {
                if(now >= sdf.parse(sTime).getTime() || now <= sdf.parse(eTime).getTime()) {
                    message = "当前时间不能缴费，请在7:00 - 22:00内操作";
                    return true;
                }
            } catch (ParseException e1) {
                log.error("创建订单日期格式化异常{}", e1);
                return true;
            }

        }
        message = "当前时间可以缴费";
        return  false;
    }




}
